﻿package mortal.game.view.wizard.panel
{
    import com.greensock.*;
    import com.greensock.easing.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.view.common.*;
    import mortal.game.view.wizard.data.*;
    import mortal.mvc.core.*;

    public class WizardTabCellRenderer extends GCellRenderer
    {
        private var _wizardIcon:GImageBitmap;
        private var _sharpBtn:GLoadingButton;
        private var _currentWizard:WizardData;
        private var _isLastSprite:Boolean;
        private var _redPoint:GBitmap;

        public function WizardTabCellRenderer()
        {
            this.setSize(200, 63);
            return;
        }// end function

        override protected function initSkin() : void
        {
            var _loc_1:* = GlobalClass.getScaleBitmap(ImagesConst.WizardBtn_disableSkin, new Rectangle(100, 31, 1, 1));
            var _loc_2:* = GlobalClass.getScaleBitmap(ImagesConst.WizardBtn_overSkin, new Rectangle(100, 31, 1, 1));
            var _loc_3:* = GlobalClass.getScaleBitmap(ImagesConst.WizardBtn_upSkin, new Rectangle(100, 31, 1, 1));
            var _loc_4:int = 40;
            _loc_3.height = 40;
            var _loc_4:* = _loc_4;
            _loc_2.height = _loc_4;
            _loc_1.height = _loc_4;
            this.setStyle("downSkin", _loc_2);
            this.setStyle("overSkin", _loc_2);
            this.setStyle("upSkin", _loc_3);
            this.setStyle("selectedDownSkin", _loc_1);
            this.setStyle("selectedOverSkin", _loc_1);
            this.setStyle("selectedUpSkin", _loc_1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this._wizardIcon = UIFactory.gImageBitmap("", 18, 0, this);
            this._wizardIcon.x = 18;
            this._wizardIcon.y = 0;
            addChild(this._wizardIcon);
            this._redPoint = UIFactory.gBitmap(ImagesConst.SmallRedPoint, 22, 5, this);
            this._redPoint.visible = false;
            this._sharpBtn = UIFactory.gLoadingButton(ResFileConst.WizardSelectedBtn, 210, 13, 58, 25, this);
            this._sharpBtn.configEventListener(MouseEvent.CLICK, this.sharpWizard);
            this._sharpBtn.drawNow();
            NetDispatcher.addCmdListener(ServerCommand.MoneyUpdate, this.updateRedPointState);
            NetDispatcher.addCmdListener(ServerCommand.WizardWinOpen, this.updateRedPointState);
            NetDispatcher.addCmdListener(ServerCommand.WizardLevelUp, this.updateRedPointState);
            NetDispatcher.addCmdListener(ServerCommand.WizardAcupointLevelUp, this.updateRedPointState);
            NetDispatcher.addCmdListener(ServerCommand.WizardSelectSuccess, this.updateSelected);
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            this._wizardIcon.dispose(param1);
            this._wizardIcon = null;
            this._redPoint.dispose(param1);
            this._redPoint = null;
            this._sharpBtn.dispose(param1);
            this._sharpBtn = null;
            NetDispatcher.removeCmdListener(ServerCommand.MoneyUpdate, this.updateRedPointState);
            NetDispatcher.removeCmdListener(ServerCommand.WizardWinOpen, this.updateRedPointState);
            NetDispatcher.removeCmdListener(ServerCommand.WizardLevelUp, this.updateRedPointState);
            NetDispatcher.removeCmdListener(ServerCommand.WizardAcupointLevelUp, this.updateRedPointState);
            NetDispatcher.removeCmdListener(ServerCommand.WizardSelectSuccess, this.updateSelected);
            super.disposeImpl(param1);
            return;
        }// end function

        private function showOver(event:MouseEvent) : void
        {
            this._wizardIcon.imgUrl = "Sprite" + this._currentWizard.code + "_overSkin";
            TweenMax.to(this, 0.3, {x:0, ease:Quint.easeOut});
            return;
        }// end function

        private function showOut(event:MouseEvent) : void
        {
            this._wizardIcon.imgUrl = "Sprite" + this._currentWizard.code + "_upSkin";
            TweenMax.to(this, 0.3, {x:25, ease:Quint.easeOut});
            return;
        }// end function

        override public function set selected(param1:Boolean) : void
        {
            if (param1)
            {
                this._isLastSprite = true;
                if (this._currentWizard.isJustOpen)
                {
                    this.x = 0;
                    this._currentWizard.isJustOpen = false;
                }
                else
                {
                    TweenMax.to(this, 0.3, {x:0, ease:Quint.easeOut});
                }
                this._wizardIcon.imgUrl = "Sprite" + this._currentWizard.code + "_overSkin";
                this.removeEventListener(MouseEvent.ROLL_OVER, this.showOver);
                this.removeEventListener(MouseEvent.ROLL_OUT, this.showOut);
            }
            else
            {
                if (this._isLastSprite)
                {
                    this.x = 0;
                    this._isLastSprite = false;
                    TweenMax.to(this, 0.3, {x:25, ease:Quint.easeOut});
                }
                else
                {
                    this.x = 25;
                }
                this._wizardIcon.imgUrl = "Sprite" + this._currentWizard.code + "_upSkin";
                this.configEventListener(MouseEvent.ROLL_OVER, this.showOver);
                this.configEventListener(MouseEvent.ROLL_OUT, this.showOut);
            }
            super.selected = param1;
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            this._currentWizard = param1.data;
            this._wizardIcon.x = (this.width - 141 >> 1) - 5;
            this._wizardIcon.y = this.height - 65 >> 1;
            if (this._currentWizard.isHasWizard)
            {
                this.filters = [];
            }
            else
            {
                this.filters = [FilterConst.colorFilter2];
            }
            this.updateRedPointState();
            this.updateSelected();
            return;
        }// end function

        private function sharpWizard(event:MouseEvent) : void
        {
            Dispatcher.dispatchEvent(new DataEvent(EventName.WizardSelectFight, this._currentWizard.code));
            return;
        }// end function

        private function updateSelected(param1:Object = null) : void
        {
            this._sharpBtn.visible = this._currentWizard.isHasWizard;
            this._sharpBtn.enabled = this._currentWizard.code != Cache.instance.wizard.fightWizardCode;
            this._sharpBtn.drawNow();
            return;
        }// end function

        private function updateRedPointState(param1:Object = null) : void
        {
            var _loc_2:Boolean = false;
            var _loc_3:* = this._currentWizard.tSprite.code;
            _loc_2 = WizardUtil.isCanActiveByCode(_loc_3);
            if (!_loc_2)
            {
                _loc_2 = WizardUtil.isCanAdvanceByCode(_loc_3);
            }
            if (!_loc_2)
            {
                _loc_2 = WizardUtil.isCanLevelUpByCode(_loc_3);
            }
            this._redPoint.visible = _loc_2;
            return;
        }// end function

    }
}
